/*
 *
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jni.h>
#include <errno.h>

#include "com_snda_setupmanager_BackupApkAdpter.h"

JNIEXPORT jstring JNICALL Java_com_snda_setupmanager_BackupApkAdpter_BackupApk
  (JNIEnv* env, jobject thiz, jstring src, jstring dest)
{
	char* srcFileName =  (*env)->GetStringUTFChars(env, src, 0);
	FILE* srcFile = fopen(srcFileName, "r+");
	if (srcFile != NULL)
	{
		char* destFileName =  (*env)->GetStringUTFChars(env, dest, 0);
		FILE* destFile = fopen(destFileName, "w+");
		char buffer[1024] = {};

		int nRead = 0;

		do
		{
			nRead = fread(buffer, sizeof(char), sizeof(buffer), srcFile);
			if (nRead > 0)
				fwrite(buffer, sizeof(char), nRead, destFile);
		} while (nRead > 0);

		fclose(srcFile);
		fclose(destFile);
	}
	else
	{
		char buffer[64];
		sprintf(buffer, "this is test! error:(%d)", errno);

		return (*env)->NewStringUTF(env, buffer);
	}

    return (*env)->NewStringUTF(env, "Hello from Liugang !");
}
